//获取应用实例
import network from '../../utils/network.js';
var app = getApp();

Page({  
    data: { 
      uid:0, 
      login: false, 
      encry: [],
      userInfo:[],
    },  
    onShow:function(){    
        app.editTabBar();    //显示自定义的底部导航  
    },   

  onLoad: function (e) {
    var that = this;  
    if (Object.keys(e).length != 0 ){
      var uid = e.id != '' ? e.id : 0;
      that.setData({
        uid: uid,
      });
    }
    wx.getStorage({
      key: 'user',
      success: function (res) {  
        if (res.data.is_user == 1) {
          wx.navigateTo({
            url: '/view/join/join'
          });
        } else {
          that.setData({
            userInfo: res.data,
            login: true,
          });
        }
      },
      fail: function (e) { 
        wx.login({
          success: function (re) {
            that.setData({
              code: re.code
            });
            that.onGetUsrCode("正在加载数据");
          }
        });
      }
    });  
  },
  // 获取删除
  onGetUsrCode: function (message) {
    var that = this;
    var data = {
      code: that.data.code,
    }; 
    network.requestLoading('http://jbc.com/login/Login/getCode', data, message, function (res) {
      if (res.code > 0) {
        if (res.data.is_user == 1) {
          wx.navigateTo({
            url: '/view/join/join'
          });
        }else{
          wx.setStorage({
            key: 'user',
            data: res.data
          })
          that.setData({
            login: true,
            userInfo: res.data
          });
        }
        //that.getSale_list('正在刷新数据')
      } else {
        // wx.showToast({
        //   title: res.msg,
        // });
      }
    }, function (res) {
      wx.showToast({
        title: '加载数据失败',
      })

    })
  },
  /*
   *获取用户授权信息
   */
  onGotUserInfo() {
    var that = this;
    wx.login({
      success: function (re) { 
        that.setData({
          code: re.code
        });
        wx.getUserInfo({
          success: function (re) {
            that.setData({
              userInfo: {
                avatar: re.avatarUrl,
                nickName: re.nickName,
              },
              encry: re
            });
            that.onGetUserInfo("正在授权中");
          }
        });
      }
    });
    
   
  },

//授权新增用户
  onGetUserInfo: function (message) {
    var that = this;
    var encry = that.data.encry; 
    var code = that.data.code;
    var data = {
      code: that.data.code,
      code: code,
      signature: encry.signature,
      rawData: encry.rawData,
      encryptData: encry.encryptedData,
      iv: encry.iv,
      cs_id:that.data.uid
    }; 
    network.requestLoading('http://jbc.com/login/Login/getUserInfo', data, message, function (res) {
      if (res.code > 0) {
        that.setData({
          login: 1,
          userInfo: res.data,
        });
        //设置标题
        wx.setNavigationBarTitle({
          title: "会员中心"
        });
      } else {
        wx.showToast({
          title: res.msg,
        });
      }
    }, function (res) {
      wx.showToast({

        title: '加载数据失败',
      })

    })
  },

})

